﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using template_dll;

namespace ServerClient
{
    public partial class InsNewPath : Form
    {
        public InsNewPath()
        {
            InitializeComponent();
        }

        private SqlConnection con = new SqlConnection(@"Server=sdata2;Database=ExchangeFilesDB;Integrated Security=True");
        public string path = "";
        public int client = 0;
        public string inout = "";
        public bool map = true;
        public bool forAll = false;
        public int pathId = 0;

        private void InsNewPath_Load(object sender, EventArgs e)
        {
            ClientsSel();
            comboBox2.Items.Add(new NewItem() { ParamID = "in", ParamComment = "входящие файлы" });
            comboBox2.Items.Add(new NewItem() { ParamID = "out", ParamComment = "исходящие файлы" });
            if (inout == "in") comboBox2.SelectedIndex = 0;
            else comboBox2.SelectedIndex = 1;
            if (path != "") textBox1.Text = path;
            if (client != 0)
                for (int i = 0; i < comboBox1.Items.Count; i++)
                    if (Convert.ToInt32((comboBox1.Items[i] as NewItem).ParamID) == client)
                    {
                        comboBox1.SelectedIndex = i;
                        break;
                    }
            if (map) radioButton4.Checked = true;
            else radioButton3.Checked = true;

            if (forAll) radioButton1.Checked = true;
            else radioButton2.Checked = true;

        }

        public void ClientsSel()
        {
            try
            {
                SPparams[] sp = new SPparams[1];
                sp[0] = new SPparams() { Name = "@closed", Value = false };
                DataTable dt = new SqlStorProc().SqlProcExecWithQueryOneTable(con, "ClientsSel", 30, sp);
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                        comboBox1.Items.Add(new NewItem() { ParamID = row[0], ParamComment = row[2].ToString() });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (con.State != ConnectionState.Closed) con.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text != "")
                {
                    if (comboBox2.SelectedIndex > -1)
                    {
                        if (pathId == 0)
                        {
                            SPparams[] sp = new SPparams[6];
                            sp[0] = new SPparams() { Name = "@path", Value = textBox1.Text };
                            sp[1] = new SPparams() { Name = "@inout", Value = (comboBox2.SelectedItem as NewItem).ParamID };
                            sp[2] = new SPparams() { Name = "@mappath", Value = radioButton4.Checked };
                            sp[3] = new SPparams() { Name = "@forallclients", Value = radioButton1.Checked };
                            sp[4] = new SPparams() { Name = "@clientorserver", Value = "s" };
                            if (comboBox1.SelectedIndex == -1) sp[5] = new SPparams() { Name = "@clientid", Value = DBNull.Value };
                            else sp[5] = new SPparams() { Name = "@clientid", Value = (comboBox1.SelectedItem as NewItem).ParamID };
                            new SqlStorProc().SqlProcExecNonQuery(con, "PathIns", 30, sp);
                            
                        }
                        else
                        {
                            SPparams[] sp = new SPparams[7];
                            sp[0] = new SPparams() { Name = "@path", Value = textBox1.Text };
                            sp[1] = new SPparams() { Name = "@inout", Value = (comboBox2.SelectedItem as NewItem).ParamID };
                            sp[2] = new SPparams() { Name = "@mappath", Value = radioButton4.Checked };
                            sp[3] = new SPparams() { Name = "@forallclients", Value = radioButton1.Checked };
                            sp[4] = new SPparams() { Name = "@clientorserver", Value = "s" };
                            if (comboBox1.SelectedIndex == -1) sp[5] = new SPparams() { Name = "@clientid", Value = DBNull.Value };
                            else sp[5] = new SPparams() { Name = "@clientid", Value = (comboBox1.SelectedItem as NewItem).ParamID };
                            sp[6] = new SPparams() { Name = "@pathid", Value = pathId };
                            new SqlStorProc().SqlProcExecNonQuery(con, "PathUpd", 30, sp);
 
                        }
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("Не указан параметр Вход/Выход");
                        comboBox2.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("Не указан путь");
                    textBox1.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (con.State != ConnectionState.Closed) con.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog f = new FolderBrowserDialog();
            //f.RootFolder = Environment.SpecialFolder.NetworkShortcuts;
            if (textBox1.Text != "") f.SelectedPath = textBox1.Text;
            else f.SelectedPath = @"\\HELPDESK";
            f.ShowNewFolderButton = true;
            if (f.ShowDialog() == DialogResult.OK) textBox1.Text = f.SelectedPath;
            f = null;
        }
    }
}
